import Server from "@/config/server";
import request from '@/utils/request';
import { showErrors, showError, showSuccess } from '@/utils/showMsg';

const state = {
  requestCode : false,
  dataLists: [],
  Fields: [],
};
const actions = {
  //列表
  getLists({commit}, page) {
    request.get(Server.action.column_list, { params: page })
    .then(response => {
      console.log(response);
        if (response.data.errorCode === 0) {
            commit("setLists", response.data.result.list);
            let fields = response.data.result.fields;
            fields.push({
                title: '操作',
                align: 'center',
                width: 180,
                scopedSlots: { customRender: 'action' },
                // fixed: 'right',
            })
            // fields[2].width = "40%"
            fields.forEach(function (item, i) {
              fields[i].dataIndex = fields[i].key
              fields[i].align = 'center';
              if(i < fields.length-2) {
                fields[i].width = item.width || 150
              }
            })
            commit("setFields", fields);
        } else {

        }
    })
  },
  //添加
  dataAdd({commit}, page) {
    commit("setErrorCode", false);    
    request.post(Server.action.column_add,  page )
    .then(response => {
      console.log(response);
      commit("setErrorCode", response.data.errorCode);
      if (response.data.errorCode === 0) {
        showSuccess(response.data.result)
      } else if(response.status === 422) {
        showErrors( response.data.errors )
      } else {
        showError( response.data.message )
      }
    })
  },
  //编辑
  dataEdit({commit}, page) {
    commit("setErrorCode", false);
    request.post(Server.action.column_update, page )
    .then(response => {
      console.log(response);
      commit("setErrorCode", response.data.errorCode);
      if (response.data.errorCode === 0) {
        showSuccess(response.data.result)
      } else {
        showError( response.data.message )
      }
    })
  },
  // ID删除 > 参数为：url/id
  dataDel({commit}, id) {
    request.get(Server.action.column_delete + id)
    .then(response => {
      console.log(response);
      if (response.data.errorCode === 0) {
        showSuccess(response.data.result)
      } else {
        showError( response.data.message )
      }
    })
  },
  // 批量删除 > 参数为：[1,2,3,4,5,6]
  // dataDel({commit}, ids) {
  //   request.get(Server.action.column_delete ,{ params: ids })
  //   .then(response => {
  //     console.log(response);
  //     if (response.data.errorCode === 0) {
  //       showSuccess(response.data.result)
  //     } else {
  //       showError( response.data.message )
  //     }
  //   })
  // },
};
const mutations = {
  setErrorCode(state, text) {
	    state.requestCode = text;
	},
	setLists(state, lists) {
	    state.dataLists = lists;
	},
  setFields(state, lists) {
	    state.Fields = lists;
	},
};
export default {
    namespaced: true,
    state,
    actions,
    mutations
};
